﻿using System;

namespace N2.BlogEngine
{
	using Items;
	using be = global::BlogEngine.Core;

	static partial class ItemExtensions
	{
		public static BePost AsN2(this be.Post post)
		{
			return AsN2(
				post,
				BlogService.CurrentBlog.Posts
					.ResolveItem<BePost>(post.Id.ToInt())
			);
		}

		public static BePost AsN2(this be.Post post, BePost receiver)
		{
			receiver.Name = post.Id.ToString();
			receiver.Title = post.Title;
			receiver["Description"] = post.Description;
			receiver["Content"] = post.Content;
			receiver.Created = post.DateCreated;//TODO implement time-zone justification
			receiver.Updated = post.DateModified;//TODO implement time-zone justification
			receiver.SavedBy = post.Author;
			receiver.Published.AdjustPublishedDate(post.IsPublished);
			receiver["IsCommentsEnabled"] = post.IsCommentsEnabled;
			receiver["Raters"] = post.Raters;
			receiver["Rating"] = (double)post.Rating;
			receiver["Slug"] = post.Slug;

			return receiver;
		}

		public static BePage AsN2(this be.Page page)
		{
			return AsN2(
				page,
				BlogService
					.CurrentBlog
					.Pages
					.ResolveItem<BePage>(
						page.Id.ToInt()));
		}

		public static BePage AsN2(this be.Page page, BePage receiver)
		{
			receiver.Name = page.Id.ToString();
			receiver.Title = page.Title;
			receiver["Description"] = page.Description;
			receiver["Content"] = page.Content;
			receiver.Created = page.DateCreated;//TODO implement time-zone justification
			receiver.Updated = page.DateModified;//TODO implement time-zone justification
			receiver["Keywords"] = page.Keywords;
			receiver.Published.AdjustPublishedDate(page.IsPublished);
			receiver["IsFrontPage"] = page.IsFrontPage;
			receiver.Visible = page.ShowInList;

			return receiver;
		}

		public static BeCategory AsN2(this be.Category category)
		{
			return AsN2(
				category,
				BlogService.CurrentBlog.Categories
					.ResolveItem<BeCategory>(category.Id.ToInt()));
		}

		public static BeCategory AsN2(this be.Category category, BeCategory receiver)
		{
			receiver.Name = category.Id.ToString();
			receiver.Title = category.Title;
			receiver["Description"] = category.Description;
			
			return receiver;
		}

		public static BeComment AsN2(this be.Comment comment, BeComment receiver)
		{
			receiver.Name = comment.Id.ToString();
			receiver.Published.AdjustPublishedDate(comment.IsApproved);
			receiver.SavedBy = comment.Author;
			receiver["Website"] = comment.Website.ToString();
			receiver["Email"] = comment.Email;
			receiver["Content"] = comment.Content;
			receiver.Created = comment.DateCreated;
			receiver["Country"] = comment.Country;
			receiver["IP"] = comment.IP;

			return receiver;
		}
	}
}
